﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using HslCommunication.BasicFramework;
using HslCommunication.Core;
using HslCommunication.Core.Net;
using HslCommunication.Reflection;

namespace HslCommunication.Robot.YAMAHA
{
	/// <summary>
	/// 雅马哈机器人的数据访问类
	/// </summary>
	// Token: 0x0200002D RID: 45
	public class YamahaRCX : NetworkDoubleBase
	{
		/// <summary>
		/// 实例化一个默认的对象
		/// </summary>
		// Token: 0x06000339 RID: 825 RVA: 0x0000CF29 File Offset: 0x0000B129
		public YamahaRCX()
		{
			base.ByteTransform = new RegularByteTransform();
			base.ReceiveTimeOut = 30000;
		}

		/// <summary>
		/// 指定IP地址和端口来实例化一个对象
		/// </summary>
		/// <param name="ipAddress">IP地址</param>
		/// <param name="port">端口号</param>
		// Token: 0x0600033A RID: 826 RVA: 0x0000CF4B File Offset: 0x0000B14B
		public YamahaRCX(string ipAddress, int port)
		{
			base.ByteTransform = new RegularByteTransform();
			this.IpAddress = ipAddress;
			this.Port = port;
			base.ReceiveTimeOut = 30000;
		}

		/// <inheritdoc />
		// Token: 0x0600033B RID: 827 RVA: 0x0000CF80 File Offset: 0x0000B180
		public override OperateResult<byte[]> ReadFromCoreServer(Socket socket, byte[] send, bool hasResponseData = true, bool usePackHeader = true)
		{
			OperateResult operateResult = base.Send(socket, send);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				bool flag2 = base.ReceiveTimeOut < 0;
				if (flag2)
				{
					result = OperateResult.CreateSuccessResult<byte[]>(new byte[0]);
				}
				else
				{
					result = base.ReceiveCommandLineFromSocket(socket, 13, 10, 60000);
				}
			}
			return result;
		}

		/// <summary>
		/// 发送命令行到socket, 并从机器人读取指定的命令行
		/// </summary>
		/// <param name="send">等待发送的数据</param>
		/// <param name="lines">接收的行数</param>
		/// <returns>结果的结果数据内容</returns>
		// Token: 0x0600033C RID: 828 RVA: 0x0000CFDC File Offset: 0x0000B1DC
		public OperateResult<string[]> ReadFromServer(byte[] send, int lines)
		{
			OperateResult<string[]> operateResult = new OperateResult<string[]>();
			OperateResult<Socket> operateResult2 = null;
			this.InteractiveLock.Enter();
			try
			{
				operateResult2 = base.GetAvailableSocket();
				bool flag = !operateResult2.IsSuccess;
				if (flag)
				{
					this.IsSocketError = true;
					AlienSession alienSession = base.AlienSession;
					if (alienSession != null)
					{
						alienSession.Offline();
					}
					this.InteractiveLock.Leave();
					operateResult.CopyErrorFromOther<OperateResult<Socket>>(operateResult2);
					return operateResult;
				}
				List<string> list = new List<string>();
				bool flag2 = false;
				for (int i = 0; i < lines; i++)
				{
					OperateResult<byte[]> operateResult3 = this.ReadFromCoreServer(operateResult2.Content, send, true, true);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						flag2 = true;
						this.IsSocketError = true;
						AlienSession alienSession2 = base.AlienSession;
						if (alienSession2 != null)
						{
							alienSession2.Offline();
						}
						operateResult.CopyErrorFromOther<OperateResult<byte[]>>(operateResult3);
						break;
					}
					list.Add(Encoding.ASCII.GetString(operateResult3.Content.RemoveLast(2)));
				}
				bool flag4 = !flag2;
				if (flag4)
				{
					this.IsSocketError = false;
					operateResult.IsSuccess = true;
					operateResult.Content = list.ToArray();
					operateResult.Message = StringResources.Language.SuccessText;
				}
				this.ExtraAfterReadFromCoreServer(new OperateResult
				{
					IsSuccess = !flag2
				});
				this.InteractiveLock.Leave();
			}
			catch
			{
				this.InteractiveLock.Leave();
				throw;
			}
			bool flag5 = !this.isPersistentConn;
			if (flag5)
			{
				if (operateResult2 != null)
				{
					Socket content = operateResult2.Content;
					if (content != null)
					{
						content.Close();
					}
				}
			}
			return operateResult;
		}

		/// <inheritdoc />
		// Token: 0x0600033D RID: 829 RVA: 0x0000D18C File Offset: 0x0000B38C
		[DebuggerStepThrough]
		public override Task<OperateResult<byte[]>> ReadFromCoreServerAsync(Socket socket, byte[] send, bool hasResponseData = true, bool usePackHeader = true)
		{
			YamahaRCX.<ReadFromCoreServerAsync>d__4 <ReadFromCoreServerAsync>d__ = new YamahaRCX.<ReadFromCoreServerAsync>d__4();
			<ReadFromCoreServerAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadFromCoreServerAsync>d__.<>4__this = this;
			<ReadFromCoreServerAsync>d__.socket = socket;
			<ReadFromCoreServerAsync>d__.send = send;
			<ReadFromCoreServerAsync>d__.hasResponseData = hasResponseData;
			<ReadFromCoreServerAsync>d__.usePackHeader = usePackHeader;
			<ReadFromCoreServerAsync>d__.<>1__state = -1;
			<ReadFromCoreServerAsync>d__.<>t__builder.Start<YamahaRCX.<ReadFromCoreServerAsync>d__4>(ref <ReadFromCoreServerAsync>d__);
			return <ReadFromCoreServerAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Robot.YAMAHA.YamahaRCX.ReadFromServer(System.Byte[],System.Int32)" />
		// Token: 0x0600033E RID: 830 RVA: 0x0000D1F0 File Offset: 0x0000B3F0
		[DebuggerStepThrough]
		public Task<OperateResult<string[]>> ReadFromServerAsync(byte[] send, int lines)
		{
			YamahaRCX.<ReadFromServerAsync>d__5 <ReadFromServerAsync>d__ = new YamahaRCX.<ReadFromServerAsync>d__5();
			<ReadFromServerAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string[]>>.Create();
			<ReadFromServerAsync>d__.<>4__this = this;
			<ReadFromServerAsync>d__.send = send;
			<ReadFromServerAsync>d__.lines = lines;
			<ReadFromServerAsync>d__.<>1__state = -1;
			<ReadFromServerAsync>d__.<>t__builder.Start<YamahaRCX.<ReadFromServerAsync>d__5>(ref <ReadFromServerAsync>d__);
			return <ReadFromServerAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Robot.YAMAHA.YamahaRCX.ReadCommand(System.String,System.Int32)" />
		// Token: 0x0600033F RID: 831 RVA: 0x0000D244 File Offset: 0x0000B444
		[DebuggerStepThrough]
		public Task<OperateResult<string[]>> ReadCommandAsync(string command, int lines)
		{
			YamahaRCX.<ReadCommandAsync>d__6 <ReadCommandAsync>d__ = new YamahaRCX.<ReadCommandAsync>d__6();
			<ReadCommandAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string[]>>.Create();
			<ReadCommandAsync>d__.<>4__this = this;
			<ReadCommandAsync>d__.command = command;
			<ReadCommandAsync>d__.lines = lines;
			<ReadCommandAsync>d__.<>1__state = -1;
			<ReadCommandAsync>d__.<>t__builder.Start<YamahaRCX.<ReadCommandAsync>d__6>(ref <ReadCommandAsync>d__);
			return <ReadCommandAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Robot.YAMAHA.YamahaRCX.Reset" />
		// Token: 0x06000340 RID: 832 RVA: 0x0000D298 File Offset: 0x0000B498
		[DebuggerStepThrough]
		public Task<OperateResult> ResetAsync()
		{
			YamahaRCX.<ResetAsync>d__7 <ResetAsync>d__ = new YamahaRCX.<ResetAsync>d__7();
			<ResetAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<ResetAsync>d__.<>4__this = this;
			<ResetAsync>d__.<>1__state = -1;
			<ResetAsync>d__.<>t__builder.Start<YamahaRCX.<ResetAsync>d__7>(ref <ResetAsync>d__);
			return <ResetAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Robot.YAMAHA.YamahaRCX.Run" />
		// Token: 0x06000341 RID: 833 RVA: 0x0000D2DC File Offset: 0x0000B4DC
		[DebuggerStepThrough]
		public Task<OperateResult> RunAsync()
		{
			YamahaRCX.<RunAsync>d__8 <RunAsync>d__ = new YamahaRCX.<RunAsync>d__8();
			<RunAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<RunAsync>d__.<>4__this = this;
			<RunAsync>d__.<>1__state = -1;
			<RunAsync>d__.<>t__builder.Start<YamahaRCX.<RunAsync>d__8>(ref <RunAsync>d__);
			return <RunAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Robot.YAMAHA.YamahaRCX.Stop" />
		// Token: 0x06000342 RID: 834 RVA: 0x0000D320 File Offset: 0x0000B520
		[DebuggerStepThrough]
		public Task<OperateResult> StopAsync()
		{
			YamahaRCX.<StopAsync>d__9 <StopAsync>d__ = new YamahaRCX.<StopAsync>d__9();
			<StopAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<StopAsync>d__.<>4__this = this;
			<StopAsync>d__.<>1__state = -1;
			<StopAsync>d__.<>t__builder.Start<YamahaRCX.<StopAsync>d__9>(ref <StopAsync>d__);
			return <StopAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Robot.YAMAHA.YamahaRCX.ReadMotorStatus" />
		// Token: 0x06000343 RID: 835 RVA: 0x0000D364 File Offset: 0x0000B564
		[DebuggerStepThrough]
		public Task<OperateResult<int>> ReadMotorStatusAsync()
		{
			YamahaRCX.<ReadMotorStatusAsync>d__10 <ReadMotorStatusAsync>d__ = new YamahaRCX.<ReadMotorStatusAsync>d__10();
			<ReadMotorStatusAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<int>>.Create();
			<ReadMotorStatusAsync>d__.<>4__this = this;
			<ReadMotorStatusAsync>d__.<>1__state = -1;
			<ReadMotorStatusAsync>d__.<>t__builder.Start<YamahaRCX.<ReadMotorStatusAsync>d__10>(ref <ReadMotorStatusAsync>d__);
			return <ReadMotorStatusAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Robot.YAMAHA.YamahaRCX.ReadModeStatus" />
		// Token: 0x06000344 RID: 836 RVA: 0x0000D3A8 File Offset: 0x0000B5A8
		[DebuggerStepThrough]
		public Task<OperateResult<int>> ReadModeStatusAsync()
		{
			YamahaRCX.<ReadModeStatusAsync>d__11 <ReadModeStatusAsync>d__ = new YamahaRCX.<ReadModeStatusAsync>d__11();
			<ReadModeStatusAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<int>>.Create();
			<ReadModeStatusAsync>d__.<>4__this = this;
			<ReadModeStatusAsync>d__.<>1__state = -1;
			<ReadModeStatusAsync>d__.<>t__builder.Start<YamahaRCX.<ReadModeStatusAsync>d__11>(ref <ReadModeStatusAsync>d__);
			return <ReadModeStatusAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Robot.YAMAHA.YamahaRCX.ReadJoints" />
		// Token: 0x06000345 RID: 837 RVA: 0x0000D3EC File Offset: 0x0000B5EC
		[DebuggerStepThrough]
		public Task<OperateResult<float[]>> ReadJointsAsync()
		{
			YamahaRCX.<ReadJointsAsync>d__12 <ReadJointsAsync>d__ = new YamahaRCX.<ReadJointsAsync>d__12();
			<ReadJointsAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<float[]>>.Create();
			<ReadJointsAsync>d__.<>4__this = this;
			<ReadJointsAsync>d__.<>1__state = -1;
			<ReadJointsAsync>d__.<>t__builder.Start<YamahaRCX.<ReadJointsAsync>d__12>(ref <ReadJointsAsync>d__);
			return <ReadJointsAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Robot.YAMAHA.YamahaRCX.ReadEmergencyStatus" />
		// Token: 0x06000346 RID: 838 RVA: 0x0000D430 File Offset: 0x0000B630
		[DebuggerStepThrough]
		public Task<OperateResult<int>> ReadEmergencyStatusAsync()
		{
			YamahaRCX.<ReadEmergencyStatusAsync>d__13 <ReadEmergencyStatusAsync>d__ = new YamahaRCX.<ReadEmergencyStatusAsync>d__13();
			<ReadEmergencyStatusAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<int>>.Create();
			<ReadEmergencyStatusAsync>d__.<>4__this = this;
			<ReadEmergencyStatusAsync>d__.<>1__state = -1;
			<ReadEmergencyStatusAsync>d__.<>t__builder.Start<YamahaRCX.<ReadEmergencyStatusAsync>d__13>(ref <ReadEmergencyStatusAsync>d__);
			return <ReadEmergencyStatusAsync>d__.<>t__builder.Task;
		}

		/// <summary>
		/// 读取指定的命令的方法，需要指定命令，和接收命令的行数信息<br />
		/// The method of reading the specified command requires the specified command and the line number information of the received command
		/// </summary>
		/// <param name="command">命令</param>
		/// <param name="lines">接收的行数信息</param>
		/// <returns>接收的命令</returns>
		// Token: 0x06000347 RID: 839 RVA: 0x0000D474 File Offset: 0x0000B674
		[HslMqttApi(Description = "The method of reading the specified command requires the specified command and the line number information of the received command")]
		public OperateResult<string[]> ReadCommand(string command, int lines)
		{
			byte[] send = SoftBasic.SpliceArray<byte>(new byte[][]
			{
				Encoding.ASCII.GetBytes(command),
				new byte[]
				{
					13,
					10
				}
			});
			return this.ReadFromServer(send, lines);
		}

		// Token: 0x06000348 RID: 840 RVA: 0x0000D4BC File Offset: 0x0000B6BC
		private OperateResult CheckResponseOk(string msg)
		{
			bool flag = msg.StartsWith("OK");
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateSuccessResult();
			}
			else
			{
				result = new OperateResult(msg);
			}
			return result;
		}

		/// <summary>
		/// 指定程序复位信息，对所有的程序进行复位。当重新启动了程序时，从主程序或者任务 1 中最后执行的程序开头开始执行。<br />
		/// Specify the program reset information to reset all programs. When the program is restarted, 
		/// execution starts from the beginning of the main program or the last executed program in task 1.
		/// </summary>
		/// <returns>执行结果是否成功</returns>
		// Token: 0x06000349 RID: 841 RVA: 0x0000D4EC File Offset: 0x0000B6EC
		[HslMqttApi(Description = "Specify the program reset information to reset all programs. When the program is restarted")]
		public OperateResult Reset()
		{
			OperateResult<string[]> operateResult = this.ReadCommand("@ RESET ", 1);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				result = this.CheckResponseOk(operateResult.Content[0]);
			}
			return result;
		}

		/// <summary>
		/// 执行程序运行。执行所有的 RUN 状态程序。<br />
		/// Execute the program to run. Execute all RUN state programs.
		/// </summary>
		/// <returns>执行结果是否成功</returns>
		// Token: 0x0600034A RID: 842 RVA: 0x0000D52C File Offset: 0x0000B72C
		[HslMqttApi(Description = "Execute the program to run. Execute all RUN state programs.")]
		public OperateResult Run()
		{
			OperateResult<string[]> operateResult = this.ReadCommand("@ RUN ", 1);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				result = this.CheckResponseOk(operateResult.Content[0]);
			}
			return result;
		}

		/// <summary>
		/// 执行程序停止。执行所有的 STOP 状态程序。<br />
		/// The execution program stops. Execute all STOP state programs.
		/// </summary>
		/// <returns>执行结果是否成功</returns>
		// Token: 0x0600034B RID: 843 RVA: 0x0000D56C File Offset: 0x0000B76C
		[HslMqttApi(Description = "The execution program stops. Execute all STOP state programs.")]
		public OperateResult Stop()
		{
			OperateResult<string[]> operateResult = this.ReadCommand("@ STOP ", 1);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				result = this.CheckResponseOk(operateResult.Content[0]);
			}
			return result;
		}

		/// <summary>
		/// 获取马达电源状态，返回的0:马达电源关闭; 1:马达电源开启; 2:马达电源开启＋所有机器人伺服开启<br />
		/// Get the motor power status, return 0: motor power off; 1: motor power on; 2: motor power on + all robot servos on
		/// </summary>
		/// <returns>返回的0:马达电源关闭; 1:马达电源开启; 2:马达电源开启＋所有机器人伺服开启</returns>
		// Token: 0x0600034C RID: 844 RVA: 0x0000D5AC File Offset: 0x0000B7AC
		[HslMqttApi(Description = "Get the motor power status, return 0: motor power off; 1: motor power on; 2: motor power on + all robot servos on")]
		public OperateResult<int> ReadMotorStatus()
		{
			OperateResult<string[]> operateResult = this.ReadCommand("@?MOTOR ", 2);
			bool flag = !operateResult.IsSuccess;
			OperateResult<int> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<int>(operateResult);
			}
			else
			{
				OperateResult operateResult2 = this.CheckResponseOk(operateResult.Content[1]);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<int>(operateResult2);
				}
				else
				{
					result = OperateResult.CreateSuccessResult<int>(Convert.ToInt32(operateResult.Content[0]));
				}
			}
			return result;
		}

		/// <summary>
		/// 读取模式状态<br />
		/// Read mode status
		/// </summary>
		/// <returns>模式的状态信息</returns>
		// Token: 0x0600034D RID: 845 RVA: 0x0000D61C File Offset: 0x0000B81C
		[HslMqttApi(Description = "Read mode status")]
		public OperateResult<int> ReadModeStatus()
		{
			OperateResult<string[]> operateResult = this.ReadCommand("@?MODE ", 2);
			bool flag = !operateResult.IsSuccess;
			OperateResult<int> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<int>(operateResult);
			}
			else
			{
				OperateResult operateResult2 = this.CheckResponseOk(operateResult.Content[1]);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<int>(operateResult2);
				}
				else
				{
					result = OperateResult.CreateSuccessResult<int>(Convert.ToInt32(operateResult.Content[0]));
				}
			}
			return result;
		}

		/// <summary>
		/// 读取关节的基本数据信息<br />
		/// Read the basic data information of the joint
		/// </summary>
		/// <returns>关节信息</returns>
		// Token: 0x0600034E RID: 846 RVA: 0x0000D68C File Offset: 0x0000B88C
		[HslMqttApi(Description = "Read the basic data information of the joint")]
		public OperateResult<float[]> ReadJoints()
		{
			OperateResult<string[]> operateResult = this.ReadCommand("@?WHERE ", 1);
			bool flag = !operateResult.IsSuccess;
			OperateResult<float[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<float[]>(operateResult);
			}
			else
			{
				result = OperateResult.CreateSuccessResult<float[]>(Enumerable.ToArray<float>(Enumerable.Select<string, float>(operateResult.Content[0].Split(new char[]
				{
					' '
				}, StringSplitOptions.RemoveEmptyEntries), (string m) => Convert.ToSingle(m))));
			}
			return result;
		}

		/// <summary>
		/// 读取紧急停止状态，0 ：正常状态、1 ：紧急停止状态<br />
		/// Read emergency stop state, 0: normal state, 1: emergency stop state
		/// </summary>
		/// <returns>0 ：正常状态、1 ：紧急停止状态</returns>
		// Token: 0x0600034F RID: 847 RVA: 0x0000D708 File Offset: 0x0000B908
		[HslMqttApi(Description = "Read emergency stop state, 0: normal state, 1: emergency stop state")]
		public OperateResult<int> ReadEmergencyStatus()
		{
			OperateResult<string[]> operateResult = this.ReadCommand("@?EMG ", 2);
			bool flag = !operateResult.IsSuccess;
			OperateResult<int> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<int>(operateResult);
			}
			else
			{
				OperateResult operateResult2 = this.CheckResponseOk(operateResult.Content[1]);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<int>(operateResult2);
				}
				else
				{
					result = OperateResult.CreateSuccessResult<int>(Convert.ToInt32(operateResult.Content[0]));
				}
			}
			return result;
		}
	}
}
